#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <pthread.h>
#include <netdb.h>
#define MAX 1000

typedef struct {
	int sockfd;
	int newsockfd;
	int n;
	int portno;
	int vet[MAX];
	int size;
} thread_info;
void error(const char *msg) {
	perror(msg);
	exit(1);
}
void sort(int v[], int n) {
	int i, aux, j;
	for (i = 0; i < n; i++) {
		for (j = 0; j < n - 1; j++) {
			if (v[j] > v[j + 1]) {
				aux = v[j];
				v[j] = v[j + 1];
				v[j + 1] = aux;
			}
		}
	}
}
void*SocketHandler(void* lp) {
	thread_info *thr = (thread_info*) lp;
	//int sockfd, newsockfd, size;
	socklen_t clilen;
	struct sockaddr_in serv_addr, cli_addr;
	int buffer[MAX], i, aux = 0, size;

	thr->sockfd = socket(AF_INET, SOCK_STREAM, 0);
	if (thr->sockfd < 0)
		error("ERROR opening socket");
	bzero((char *) &serv_addr, sizeof(serv_addr));
	//	thr->portno = atoi(argv[1]);
	serv_addr.sin_family = AF_INET;
	serv_addr.sin_addr.s_addr = INADDR_ANY;
	serv_addr.sin_port = htons(thr->portno);
	if (bind(thr->sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr))
			< 0)
		error("ERROR on binding");
	listen(thr->sockfd, 5);
	clilen = sizeof(cli_addr);
	thr->newsockfd
			= accept(thr->sockfd, (struct sockaddr *) &cli_addr, &clilen);
	if (thr->newsockfd < 0)
		error("ERROR on accept");
	bzero(buffer, sizeof(int) * MAX);
	thr->n = read(thr->newsockfd, buffer, sizeof(int) * MAX);
	if (thr->n < 0)
		error("ERROR reading from socket");
	size = thr->n / sizeof(int);
	for (i = 0; i < size; i++) {
		thr->vet[aux] = buffer[i];
		aux++;
	}
	thr->size = size;
	//thr->n = write(thr->newsockfd, "\nI got your message", 20);
	if (thr->n < 0)
		error("ERROR writing to socket");

	printf("\nVetor Original:\n");
	for (i = 0; i < size; i++) {
		printf("%d ", thr->vet[i]);
	}
	return 0;
}

int main(int argc, char *argv[]) {

	int i, j, vet[MAX];
	pthread_t thread_id1 = 0, thread_id2 = 0;
	thread_info thr1, thr2;
	/*if (argc < 3) {
	 fprintf(stderr, "ERROR, no port provided\n");
	 exit(1);
	 }*/
	/**** Codigo para receber do cliente os vetores ordenalos******/
	thr1.portno = atoi(argv[1]);
	thr2.portno = atoi(argv[2]);
	pthread_create(&thread_id1, NULL, &SocketHandler, (void*) &thr1);
	pthread_create(&thread_id2, NULL, &SocketHandler, (void*) &thr2);

	pthread_join(thread_id1, NULL);
	pthread_join(thread_id2, NULL);

	for (i = 0; i < thr1.size; i++) {
		vet[i] = thr1.vet[i];
	}
	j = 0;
	for (i = thr1.size; i < thr2.size + thr1.size; i++) {
		vet[i] = thr2.vet[j];
		j++;
	}
	printf("\nVetor Ordenado:\n");
	sort(vet, thr2.size + thr1.size);
	for (i = 0; i < thr2.size + thr1.size; i++) {
		printf("%d ", vet[i]);
	}
	/****************************************************************/
	/****Codigo para devolver o vetor para o cliente*****************/
	thr1.n = write(thr1.newsockfd, vet, (sizeof(int)*thr2.size + sizeof(int)*thr1.size));
	thr2.n = write(thr2.newsockfd, vet, (sizeof(int)*thr2.size + sizeof(int)*thr1.size));
	/****************************************************************/

	return 0;
}
